import {_decorator, Component, Label} from 'cc';
import {EventManager} from "db://assets/Scripts/Common/EventManager";
import {EventType} from "db://assets/Scripts/Common/EventType";
import {App} from "db://assets/Scripts/Common/App";
import {BlockType} from "db://assets/Scripts/Player/BlockType";
import {GameState} from "db://assets/Scripts/Game/GameState";

const {ccclass, property} = _decorator;

/**
 * 分数控制器
 */
@ccclass('ScoreController')
export class ScoreController extends Component {
    /**
     * 分数文本
     */
    private scoreLabel: Label = null!;

    onLoad() {
        // 初始化分数文本
        this.scoreLabel = this.node.getComponent(Label);
    }

    onEnable() {
        // 注册更新分数监听事件
        EventManager.on(EventType.SCORE, this.updateScore, this);
    }

    onDisable() {
        // 注销更新分数监听事件
        EventManager.off(EventType.SCORE, this.updateScore, this);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    /**
     * 更新分数
     *
     * @param score 分数
     */
    updateScore(score: number) {
        // 显示分数
        this.scoreLabel.string = `${score > App.ROAD_LENGTH ? App.ROAD_LENGTH : score}`;
    }
}